package com.david.flowable.service.Impl;


import com.david.flowable.flowfactory.FlowServiceFactory;
import com.david.flowable.service.FlowInstanceService;
import com.david.system.mapper.UserMapper;
import com.david.system.pojo.Entity.UserEntity;
import org.flowable.bpmn.model.StartEvent;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.flowable.task.api.history.HistoricTaskInstanceQuery;
import org.flowable.variable.api.history.HistoricVariableInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static org.springframework.security.core.Authentication.*;

@Service
public class FlowInstanceServiceImpl extends FlowServiceFactory implements FlowInstanceService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public String startFlowInstanceById(String flowId, Map<String, Object> variables) {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        UserEntity userEntity = userMapper.selectUserByUsername(username);
        variables.put("initiator", userEntity.getId().toString());
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(flowId, variables);
        List<Task> taskList = taskService.createTaskQuery().processInstanceId(processInstance.getId()).list();
//        for (Task task : taskList) {
//            if (Objects.equals(task.getAssignee(), username)) {
//                taskService.setAssignee(task.getId(), userEntity.getId().toString());
//            }
//        }

        System.out.println(processInstance.getStartUserId() + "启动流程");
        return processInstance.getProcessInstanceId();
    }

    @Override
    public Map<String, Object> getStartForm(String procInsId) {
        // 完成的任务无法获取流程变量,疑似可以获取startEvent所有的virables
        Map<String, Object> variables = new HashMap<>();
        List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery().processInstanceId(procInsId).list();
        for (HistoricActivityInstance historicActivityInstance : list) {
            if (Objects.equals(historicActivityInstance.getActivityId(), "start_event")) {
                System.out.println(historicActivityInstance);
                List<HistoricVariableInstance> list1 = historyService.createHistoricVariableInstanceQuery().processInstanceId(procInsId).list();
                System.out.println(list1);
                for (HistoricVariableInstance hiv : list1) {
                    variables.put(hiv.getVariableName(), hiv.getValue());
                }
            }
        }
        System.out.println(variables);
        variables.remove("initiator");
        System.out.println("历史任务的运行参数：" + variables);
        // 正常情况
        return variables;
    }
}
